﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using forum.ApplicationCode;
using forum.Data;

namespace forum
{
    public partial class Post : System.Web.UI.Page
    {
        int PostId
        {
            get
            {
                if (Request.QueryString["PostId"] != null)
                {
                    int postId;
                    if (int.TryParse(Request.QueryString["PostId"], out postId))
                    {
                        return postId;
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // Reload the page, not from broswer's cache
            Response.AppendHeader("Cache-Control", "no-store");

            if (Membership.GetUserName() == "")
                lnkbtnAddPost.Visible = false;
            hdnfldPostId.Value = PostId.ToString();

            using (forumDataContext data = new forumDataContext())
            {
                var postDetalis = data.sp_getPostFullDetails(PostId);
                if (postDetalis == null)
                    Response.Redirect("forumWelcome.asp");
                var post = postDetalis.FirstOrDefault();
                
                lblSubForumeName.Text = "Sub forum is: " + post.subForumTopic;
                
                //thread details
                var thread = data.sp_getThreadFullDetails(post.threadId).FirstOrDefault();
                lblThreadUserName.Text= thread.userName;
                lblThreadCreationDate.Text = thread.creationDate.ToString() ;
                lblThreadTitle.Text=thread.threadTitle;
                lblThreadContent.Text = thread.threadContent;

                //post details
               lblPostUserName.Text= data.sp_GetUserNameByUserId(post.ownerId).FirstOrDefault().userName;
               lblPostCreationDate.Text= post.creationDate.ToString();
               lblPostTitile.Text= post.postTitle;
                lblPostContent.Text= post.postContent;

                lvPosts.DataSource = data.sp_GetPostPosts(PostId);
                lvPosts.DataBind();
    
            
            }

        }

        protected void lvPosts_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            using (forumDataContext data = new forumDataContext())
            {
                int postId = ((sp_GetPostPostsResult)DataBinder.GetDataItem(e.Item)).postId;
                int ownerId = ((sp_GetPostPostsResult)DataBinder.GetDataItem(e.Item)).ownerId;

                Label uName = e.Item.FindControl("lblUserName") as Label;
                uName.Text = data.sp_GetUserNameByUserId(ownerId).FirstOrDefault().userName;
                HyperLink linkEdit = e.Item.FindControl("hyplnkEdit") as HyperLink;
                HyperLink linkdel = e.Item.FindControl("lbkbtnDelete") as HyperLink;

                if (!isAllowEdit(postId))
                {
                    linkEdit.Visible = false;
                    linkdel.Visible = false;

                }

            }

        }


        protected Boolean isAllowEdit(int postId)
        {
            using (forumDataContext data = new forumDataContext())
            {

                //only an owenr allow to edit a messgage / post

                Boolean isOwner = false;

                if (data.sp_IsOwnerOfPost(Membership.GetUserId(), postId).FirstOrDefault().isOwner > 0)
                    isOwner = true;
                return (isOwner);



               /* Boolean isAdmin = Membership.IsAdmin();

                int subForumId = data.sp_getPostFullDetails(PostId).FirstOrDefault().subForumId;
                Boolean isModerator = Membership.IsModerator(subForumId);
                Boolean isOwner = false;

                if (data.sp_IsOwnerOfPost(Membership.GetUserId(), postId).FirstOrDefault().isOwner > 0)
                    isOwner = true;
                return (isAdmin || isModerator || isOwner);
                * */

            }


        } 
    }
}